package com.laikan.reader.bookreader.engines;

import android.content.Context;
import android.graphics.*;
import android.view.View;

/**
 * Created by wx
 *
 * @date 2016/5/25/15:07
 * @description
 */
public class PageView extends View {

    Context context;
    PageFactory pageFactory;
    int index;
    private ChapterStatus chapter;
    private boolean loadData = false;
    PageStyleSet css;
    public PageView(Context context, ChapterStatus chapter, PageFactory pageFactory, int index,PageStyleSet css) {
        super(context);
        this.context = context;
        this.chapter = chapter;
        this.pageFactory = pageFactory;
        this.index = index;
        this.css = css;
        setLayerType(LAYER_TYPE_SOFTWARE, null);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));
        DrawBg(canvas);
        Paint paint = new Paint();
        paint.setColor(0xff000000);
        paint.setTextSize(16 * PageStyleSet.density);
        if(loadData) {
            canvas.drawText("数据正在加载中，请稍等", 0, paint.getTextSize(), paint);
            loadData = false;
            return;
        }

        if(chapter != null) {
            if(index >= 0) {
                pageFactory.draw(canvas, chapter, index);
            }
        }

    }

    public void DrawBg(Canvas c) {

        if (css.bg == null) {
            c.drawColor(css.bgColor);
        }else {
            c.drawBitmap(css.bg, 0, 0, null);
        }

    }
    public void loadData() {
        loadData = true;
        invalidate();
    }

    /**
     * 刷新数据
     *
     * @param index
     */
    public void afresh(int index) {
        this.index = index;
        this.invalidate();
    }

    /**
     * 设置数据
     *
     * @param chapter1
     */
    public void setPages(ChapterStatus chapter1) {
        this.chapter = chapter1;
    }
}
